package com.jeect.productweb.web;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.jeect.base.domain.Product;
import com.jeect.base.util.ResponseBody;
import com.jeect.base.util.ResponseCode;
import com.jeect.productweb.service.ProductRemoteService;
import com.jeect.productweb.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ProductController {
    @Autowired
    private ProductService productService;

    @GetMapping("/product/{sku}")
    public ResponseBody getProduct(@PathVariable String sku){
        Product product = productService.getBySku(sku);

        /*if(body.getCode().equals(ResponseCode.SUCCESS.getCode())){
            ObjectMapper mapper = new ObjectMapper();
            Product product = mapper.convertValue(body.getObj(), Product.class);
            body.setObj(product);
        }
        return body;*/
        if(product!=null){
            return new ResponseBody(ResponseCode.SUCCESS,product);
        }
        return new ResponseBody(ResponseCode.FAILE);
    }
}
